﻿var trunPageObj;//翻页组件对像
var fromEleIds = "#tradeDate,#curTypeSipf,#oppPar,#mktCodeSipf,#arapBusiType,#arapAmt,#harapDate,#arapDate,#summary,#taCode,#arapDateNull";
var isModify;//是否修改
$(initPage);
function initPage() {
	//初始化翻页组件
    trunPageObj =  $(".pageNext").turnPage();
    
	$("td", "#accrualList").click(function() {
		if($("#btnSave").attr("disabled") == false) {
			if (!confirm("当前编内容还未保存，是否继续？")) {
				return;
			}
		}
		
		$(".selectTD", "#accrualList").removeClass("selectTD").find("img").attr("src","themes/comm/images/close.gif");
		$(this).addClass("selectTD").find("img").attr("src","themes/comm/images/open.gif");
		
		$("#arapType").val($(this).attr("arapType"));
		var arapType_text = $("span", this).text();
		$("#arapType_text").text(arapType_text ? arapType_text : "");
		
		$("#arapReason").val($(this).attr("arapReason"));
		
//		$("#tradeDate").val($("#busiDate").val());
//		$("#tradeDate").val("1900-01-01");
		
		
		$("#btnCancle").click();//重置状态
		trunPageObj.gotoPage(1);//搜索第一页
	});
	$("td", "#accrualList").eq(0).click();//默认打开第一个
	
	$("#busiDate").change(function() {
		$(".selectTD", "#accrualList").click();
	});
	
	//对方机构为TA代码时显示输入框
	$("#oppPar")[0].onchange = function() {
		if (this.value == "TA") {
			$("#taCode").show();
		} else {
			$("#taCode").hide();
		}
	};
	
	
	$("#arapDateNull").click(function(){
		$("#arapDate").attr("readonly", $(this).attr("checked"));
	});
	
	
	//新增
	$("#btnAdd").click(function() {
		var arapType = $(".selectTD", "#accrualList").attr("arapType");
		if (arapType == "") {
			alert("请选择应收应付类型再新增");
			return;
		}
		$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled", false);
		$(fromEleIds).attr("disabled", false);
		$("#arapDateNull").click();
		isModify = false;
		clearValues();
	});
	
	//修改
	$("#btnEdit").click(function() {
		var arapType = $(".selectTD", "#accrualList").attr("arapType");//arapType为空说明是汇总列表
		if ($("#accrualManType").val() == "1" && arapType == "") {
			alert("按明细维护方式不能修改汇总数据，请到具体类型中修改明细数据。");
			return;
		}
		$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled", false);
		$(fromEleIds).attr("disabled", false);
		
		if ($("#arapDateNull").attr("checked")) {
			$("#arapDate").attr("readonly",true);
		}
		isModify = true;
	});
	
	//取消
	$("#btnCancle").click(function() {
		$("#btnEdit,#btnDelete,#btnSave,#btnCancle").attr("disabled", true);
		$("#btnAdd").attr("disabled", false);
		$(fromEleIds).attr("disabled", true);
		clearValues();
	});
	
	//保存
	$("#btnSave").click(function() {
		saveAccrualInfo();
	});
	
	//删除
	$("#btnDelete").click(function() {
		delAccrualInfo();
	});
}

//清除表单值
function clearValues() {
	$(fromEleIds).each(function() {
		var d_val = $(this).attr("default-value");
		$(this).val(d_val ? d_val : "");
		
	});
	$("#arapDateNull").attr("checked",true);
	$("tr","#data_list").attr("bgColor","#FFFFFF");
}

//保存
function saveAccrualInfo() {
	if($.validInput("tradeDate", "交易日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("curTypeSipf", "币种", true)){
		return false;
	}
	if($.validInput("oppPar", "对方机构", true)){
		return false;
	}
	var oppPar = $.trim($("#oppPar").val());
	if (oppPar =="TA") {
		if($.validInput("taCode", "TA代码", true, "\<\>\'\"", 3)){
			return false;
		}
		oppPar = $.trim($("#taCode").val());
	}
	if($.validInput("mktCodeSipf", "市场代码", true)){
		return false;
	}
	if($.validInput("arapBusiType", "业务类型", true)){
		return false;
	}
	if($.validNumber("arapAmt", "应收应付金额", true, 999999999999999)){
		return false;
	}
	if($.validInput("harapDate", "已收付日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("arapDate", "应收付日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("summary", "摘要信息", false, "", 120)){
		return false;
	}
	
	var arapDate = "";
	if($("#arapDateNull").attr("checked") == false) {
		arapDate = $.trim($("#arapDate").val())
	}
	
	var action = isModify ? "modify" : "add";
	$.post("m/accrual_info/" + action, {
			id : $.trim($("#aiId").val()),
			arapType :$.trim($("#arapType").val()),
			tradeDate:$.trim($("#tradeDate").val()),
			oppPar :oppPar,
			mktCodeSipf :$.trim($("#mktCodeSipf").val()),
			arapBusiType:$.trim($("#arapBusiType").val()),
			curTypeSipf:$.trim($("#curTypeSipf").val()),
			arapReason:$.trim($("#arapReason").val()),
			arapAmt:$.trim($("#arapAmt").val()),
			harapDate:$.trim($("#harapDate").val()),
			arapDate:arapDate,
			summary:$.trim($("#summary").val()),
			busiDate:$.trim($("#busiDate").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert("系统提示：保存成功！");
				$("#btnCancle").click();
				$(".selectTD", "#accrualList").click();
			} 
		}, "json");
}

//删除
function delAccrualInfo() {
		if (!confirm("确定要删除记录吗？")) {
		return;
	}
	$.post("m/accrual_info/delete", {
			id :$.trim($("#aiId").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				$("#btnCancle").click();
				$(".selectTD", "#accrualList").click();
			} 
		}, "json");
}

function queryList() {
	var pageno=trunPageObj.getCurrentPageNo();//当前第几页
	var pagecount=trunPageObj.getPageCount();//一页多少条
	var busiDate = $("#busiDate").val();
	var arapType = $("#arapType").val();
	$.post("m/accrual_info/query", {
		pageNo: pageno,
		pageSize : pagecount,
		busiDate : busiDate,
		arapType : arapType
	}, function(data) {
		if ($.checkErrorMsg(data) ) {
			var fileList = "";
			if(data.accrualInfoPage && data.accrualInfoPage.dataList){
				var dataList = data.accrualInfoPage.dataList;
				$(dataList).each(function(i) {
					fileList += "<tr class='table_td_font' bgColor='#ffffff' onclick='selectTR(this)' aiid='" + dataList[i].id + "'>";
					fileList += "<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].arapType + "'>" + dataList[i].arapTypeText + "</td>";
					fileList += "<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].arapReason + "'>" + dataList[i].arapReasonText + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].formatTradeDate + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].curTypeSipf + "'>" + dataList[i].curTypeSipfText + "</td>";
					fileList += "<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].oppPar + "'>" + dataList[i].oppParText + "</td>";
					fileList += "<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].mktCodeSipf + "'>" + dataList[i].mktCodeSipfText + "</td>";
					fileList += "<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].arapBusiType + "'>" + dataList[i].arapBusiTypeText + "</td>";
					fileList += "<td align='right'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].arapAmtText + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].formatHarapDate + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].formatArapDate + "</td>";
					fileList += "<td align='left'  height='25px' class='queryList_linebg' nowrap>" + (dataList[i].summary ? dataList[i].summary : "")  + "</td>";
					fileList += "</tr>";
				});
			}
			$("#data_list").html(fileList);
			trunPageObj.setPageData(data.accrualInfoPage);
		} 
	}, "json");
}

function selectTR(obj) {
	$(obj).attr("bgColor","#FAEBD7").siblings().attr("bgColor","#FFFFFF");
	$("#aiId").val($(obj).attr("aiid"));
	var tds = $(obj).find("td");
	$("#arapType").val(tds.eq(0).attr("value"));
	$("#arapType_text").text(tds.eq(0).text());
	
	$("#arapReason").val(tds.eq(1).attr("value"));
	$("#tradeDate").val(tds.eq(2).text());
	$("#curTypeSipf").val(tds.eq(3).attr("value"));
	
	var oppPar = tds.eq(4).attr("value");
	var oppParText = tds.eq(4).text();
	if (oppParText.indexOf("TA代码:") == 0) {
		$("#oppPar").val("TA");
		$("#taCode").val(oppPar);
	} else {
		$("#oppPar").val(oppPar);
	}
	
	$("#mktCodeSipf").val(tds.eq(5).attr("value"));
	$("#arapBusiType").val(tds.eq(6).attr("value"));
	$("#arapAmt").val(tds.eq(7).text());
	$("#harapDate").val(tds.eq(8).text());
	var arapDate = tds.eq(9).text();
	$("#arapDateNull").attr("checked",arapDate == "");
	$("#arapDate").val(arapDate);
	
	$("#summary").val(tds.eq(10).text());
	
	$("#btnSave,#btnCancle").attr("disabled", true);
	$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", false);
	$(fromEleIds).attr("disabled", true);
}